package com.cn.action;

import com.cn.vo.CommtypeImg;
import com.cn.voservice.CommtypeImgService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;


public class IndexShowAction implements ActionInterface {
    CommtypeImgService cs = new CommtypeImgService();

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        Map<String, List<CommtypeImg>> map = new LinkedHashMap<>();
        List<CommtypeImg> meau = cs.queryAll();
        List<CommtypeImg> meau2 = new ArrayList<CommtypeImg>();
        List<CommtypeImg> list3 = new ArrayList<CommtypeImg>();
        Map<String, CommtypeImg> allCate = new LinkedHashMap<>();
        Map<String, List<CommtypeImg>> map1 = new LinkedHashMap<>();
        for (CommtypeImg category : meau) {
            String parent_id = category.getFather_id();
            if (map.containsKey(parent_id)) {
                List<CommtypeImg> list = map.get(parent_id);
                list.add(category);
                map.put(parent_id, list);
            } else {
                List<CommtypeImg> list = new ArrayList<>();
                list.add(category);
                map.put(parent_id, list);
            }

            if (parent_id.equals("0")) {
                meau2.add(category);
            }

            allCate.put(category.getCommodity_type_id(), category);

        }
        ServletContext application = request.getServletContext();
        application.setAttribute("meau", meau2);
        application.setAttribute("meaumap", map);
        application.setAttribute("allCate", allCate);
        application.setAttribute("meau3", meau);
        return "index.jsp";
    }
}
